package com.yungu.swift.order.processor;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.model.dto.DriverCommissionConfigDto;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.FinanceConstant;
import com.yungu.swift.order.model.vo.PayVo;
import com.yungu.swift.system.finance.model.dto.FinanceOrderDto;
import com.yungu.swift.system.finance.service.FinanceOrderService;
import com.yungu.swift.system.sys.model.dto.*;
import com.yungu.swift.system.sys.service.*;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @author Zerrion
 * @date 2017/5/8
 * 抽成处理器
 */
@Component
public class CommissionProcessor implements PayProcessor {
    private static final Logger logger = LoggerFactory.getLogger(CommissionProcessor.class);
    private static final Integer COMMISSION_LEVEL_PLATFORM = 1;
    private static final Integer COMMISSION_LEVEL_AGENT = 2;
    private static final Integer COMMISSION_LEVEL_COMPANY = 3;
    private static List<Integer> commissionLevelList = Arrays.asList(COMMISSION_LEVEL_PLATFORM, COMMISSION_LEVEL_AGENT,
            COMMISSION_LEVEL_COMPANY);

    @Reference
    private SysBusinessOrganizationService sysBusinessOrganizationService;
    @Reference
    private DriverService driverService;
    @Reference
    private PassengerService passengerService;
    @Reference
    private SysAccountPlatformService sysAccountPlatformService;
    @Reference
    private SysAccountAgentService sysAccountAgentService;
    @Reference
    private SysAccountCompanyService sysAccountCompanyService;
    @Reference
    private SysAccountDriverService sysAccountDriverService;
    @Reference
    private FinanceOrderService financeOrderService;

    @Override
    public void process(PayVo payVo) {
        // 最新的订单费用
        BigDecimal orderFare = Optional.ofNullable(payVo.getOrderFareDto().getLatestOrderFare()).orElse(BigDecimal.ZERO);
        // 订单费用金额
        BigDecimal orderBalance = BigDecimal.valueOf(payVo.getOrderFareDto().getTotalFare());

        logger.info(payVo.getDescription() + "--抽成开始--订单金额--" + orderFare.toString());

        // 平台账户
        ResponseData<SysAccountPlatformDto> getSysAccountPlatform = sysAccountPlatformService.getByAppid(payVo.getAppid());
        if (!getSysAccountPlatform.isSuccess() || getSysAccountPlatform.getData() == null) {
            logger.error("获取不到平台账户信息，appid:{}", payVo.getAppid());
            return;
        }
        SysAccountPlatformDto sysAccountPlatformDto = getSysAccountPlatform.getData();
        // 平台余额
        BigDecimal platformBalance = Optional.ofNullable(sysAccountPlatformDto.getCashBalance()).orElse(BigDecimal.ZERO);

        /**
         * 当司机单独配置抽佣规则时 使用单独规则 否则使用系统规则
         */
        Map<Integer, SysBusinessOrganizationDto> commissionMap = new HashMap<>(4);
        if(payVo.getDriverCommissionConfigDto()!=null){
            DriverCommissionConfigDto driverCommissionConfigDto = payVo.getDriverCommissionConfigDto();
            commissionMap = buildDriverCommissionMap(driverCommissionConfigDto);
        }else{
            //使用系统佣金配置
            ResponseData<List<SysBusinessOrganizationDto>> getCommission = sysBusinessOrganizationService.getCommission(
                    payVo.getDriverVo().getUuid(), payVo.getOrderDto().getTypeModule());
            if (!getCommission.isSuccess() || CollectionUtils.isEmpty(getCommission.getData())) {
                logger.warn("获取不到抽成信息，orderUuid:{}", payVo.getOrderDto().getUuid());
                return;
            }
            for (SysBusinessOrganizationDto commissionDto : getCommission.getData()) {
                if (commissionLevelList.contains(commissionDto.getLevel()) && commissionDto.getCommission() != null) {
                    commissionMap.put(commissionDto.getLevel(), commissionDto);
                }
            }
        }
        logger.info("---1. 平台抽成开始，orderUuid:{}", payVo.getOrderDto().getUuid());
        BigDecimal platformCommissionAmount = platformCommissionProcessor(commissionMap, sysAccountPlatformDto, payVo, orderFare, orderBalance);

        logger.info("---2. 代理抽成开始，orderUuid:{}", payVo.getOrderDto().getUuid());
        BigDecimal agentCommissionAmount = agentCommissionProcessor(commissionMap, sysAccountPlatformDto, payVo,
                orderFare, orderBalance);

        logger.info("---3. 公司抽成开始，orderUuid:{}", payVo.getOrderDto().getUuid());
        BigDecimal companyCommissionAmount = companyCommissionProcessor(commissionMap, sysAccountPlatformDto, payVo,
                orderFare, orderBalance);

        logger.info("---4. 司机抽成开始，orderUuid:{}", payVo.getOrderDto().getUuid());
        //抽成结束后就是司机收入
        if (orderBalance.compareTo(BigDecimal.ZERO) != 1) {
            logger.info(payVo.getDescription() + "--司机收入不执行--订单余额已被抽完");
            payVo.setOrderBalance(BigDecimal.ZERO);
            return;
        }
        //记录司机收入流水
        ResponseData<SysAccountDriverDto> driverWrapper = sysAccountDriverService.get(payVo.getDriverVo().getUuid());
        if (!driverWrapper.isSuccess() || driverWrapper.getData() == null) {
            logger.warn("获取不到司机账户信息，处理司机账户结束，driverUuid:{}", payVo.getDriverVo().getUuid());
            return;
        }
        SysAccountDriverDto accountDriverDto = driverWrapper.getData();
        // 计算司机收入
        BigDecimal amount = orderBalance.subtract(platformCommissionAmount).subtract(agentCommissionAmount)
                .subtract(companyCommissionAmount);
        //更新司机账户信息
        accountDriverDto.setFrozenBalance(handleDriverAmount(accountDriverDto, payVo, amount));
        accountDriverDto.setUpdateOn(new Date());
        CompletableFuture.runAsync(() -> sysAccountDriverService.edit(accountDriverDto)).whenComplete(
                (t, throwable) -> logger.info("订单处理完毕，更新司机账户成功，orderUuid：{}", payVo.getOrderUuid())
        );
        // 计算平台余额
        platformBalance = platformBalance.subtract(agentCommissionAmount).subtract(companyCommissionAmount).subtract(amount);
        sysAccountPlatformDto.setCashBalance(platformBalance);
        sysAccountPlatformDto.setUpdateOn(new Date());
        CompletableFuture.runAsync(() -> sysAccountPlatformService.edit(sysAccountPlatformDto)).whenComplete(
                (t, throwable) -> logger.info("订单处理完毕，更新平台账户成功，orderUuid：{}", payVo.getOrderUuid())
        );
        //设置订单余额(即司机收入)
        payVo.setOrderBalance(amount);
        logger.info(payVo.getDescription() + "--处理抽成逻辑完毕");
    }

    /**
     * 设置司机佣金规则
     * @param driverCommissionConfigDto
     * @return
     */
    private Map<Integer, SysBusinessOrganizationDto> buildDriverCommissionMap(DriverCommissionConfigDto driverCommissionConfigDto){
        Map<Integer, SysBusinessOrganizationDto> commissionMap = new HashMap<>(4);
        //平台佣金
        if(driverCommissionConfigDto.getPlatformCommissionType()!=null){
            SysBusinessOrganizationDto sysBusinessOrganizationDto = new SysBusinessOrganizationDto();
            sysBusinessOrganizationDto.setCommission(driverCommissionConfigDto.getPlatformCommission());
            sysBusinessOrganizationDto.setCommissionType(driverCommissionConfigDto.getPlatformCommissionType());
            sysBusinessOrganizationDto.setAdditionalFee(0d);
            commissionMap.put(COMMISSION_LEVEL_PLATFORM,sysBusinessOrganizationDto);
        }
        //代理佣金
        if(driverCommissionConfigDto.getPlatformCommissionType()!=null){
            SysBusinessOrganizationDto sysBusinessOrganizationDto = new SysBusinessOrganizationDto();
            sysBusinessOrganizationDto.setCommission(driverCommissionConfigDto.getAgentCommission());
            sysBusinessOrganizationDto.setCommissionType(driverCommissionConfigDto.getAgentCommissionType());
            sysBusinessOrganizationDto.setAdditionalFee(0d);
            commissionMap.put(COMMISSION_LEVEL_AGENT,sysBusinessOrganizationDto);
        }
        //公司佣金
        if(driverCommissionConfigDto.getPlatformCommissionType()!=null){
            SysBusinessOrganizationDto sysBusinessOrganizationDto = new SysBusinessOrganizationDto();
            sysBusinessOrganizationDto.setCommission(driverCommissionConfigDto.getCompanyCommission());
            sysBusinessOrganizationDto.setCommissionType(driverCommissionConfigDto.getCompanyCommissionType());
            sysBusinessOrganizationDto.setAdditionalFee(0d);
            commissionMap.put(COMMISSION_LEVEL_COMPANY,sysBusinessOrganizationDto);
        }
        return commissionMap;
    }

    private BigDecimal handleDriverAmount(SysAccountDriverDto accountDriverDto, PayVo payVo, BigDecimal amount) {

        //司机收入都是先放入冻结余额
        BigDecimal frozenBalance = ParamUtil.defaultIfNull(accountDriverDto.getFrozenBalance(), BigDecimal.ZERO);

        //公司收入抽成流水
        FinanceOrderDto driverFinance = new FinanceOrderDto();
        driverFinance.setUuid(StringUtils.buildUUID());
        driverFinance.setBusinessUuid(payVo.getOrderUuid());
        driverFinance.setAppid(payVo.getAppid());
        driverFinance.setOrderUuid(payVo.getOrderUuid());
        driverFinance.setCourse(FinanceConstant.DRIVER_EXTRACT);
        driverFinance.setAmount(amount);
        driverFinance.setFinanceNumber(StringUtils.buildUUID());
        // 平台 -> 司机账户
        driverFinance.setFinanceInflow(payVo.getDriverVo().getUuid());
        driverFinance.setFinanceOutflow(accountDriverDto.getUuid());
        driverFinance.setDescription("平台支付司机抽成" + amount + "元给司机");
        driverFinance.setHappenTime(new Date());
        driverFinance.setCreateOn(new Date(System.currentTimeMillis() + 500));
        driverFinance.setStatus(FinanceConstant.FROZEN_BALANCE_STATUS);
        driverFinance.setCreateBy(payVo.getUserUuid());
        driverFinance.setStartBalance(frozenBalance);
        //计算司机余额
//        frozenBalance = frozenBalance.add(amount).setScale(1, BigDecimal.ROUND_HALF_UP);
        frozenBalance = frozenBalance.add(amount).setScale(2, BigDecimal.ROUND_HALF_UP);
        driverFinance.setFinalBalance(frozenBalance);
        financeOrderService.add(driverFinance);
        return frozenBalance;
    }

    private BigDecimal companyCommissionProcessor(Map<Integer, SysBusinessOrganizationDto> commissionMap, SysAccountPlatformDto
            sysAccountPlatformDto, PayVo payVo, BigDecimal orderFare, BigDecimal orderBalance) {
        // 订单金额小于等于0
        if (orderBalance.compareTo(BigDecimal.ZERO) != 1) {
            logger.info(payVo.getDescription() + "--公司抽成不执行--订单余额已被抽完");
            return BigDecimal.ZERO;
        }
        if (!commissionMap.containsKey(COMMISSION_LEVEL_COMPANY)) {
            logger.info(payVo.getDescription() + "--公司抽成不执行--没有获取到公司抽成配置");
            return BigDecimal.ZERO;
        }
        SysBusinessOrganizationDto commissionDto = commissionMap.get(COMMISSION_LEVEL_COMPANY);
        //1. 计算抽成金额
        BigDecimal commissionAmount;
        if (SysBusinessOrganizationDto.COMMISSION_TYPE_PERCENT == commissionDto.getCommissionType()) {
//            commissionAmount = orderFare.multiply(BigDecimal.valueOf(commissionDto.getCommission()).multiply(new BigDecimal("0.01"))).setScale(1, BigDecimal.ROUND_HALF_UP);
            commissionAmount = orderFare.multiply(BigDecimal.valueOf(commissionDto.getCommission()).multiply(new BigDecimal("0.01"))).setScale(2, BigDecimal.ROUND_HALF_UP);
        } else {
            commissionAmount = BigDecimal.valueOf(commissionDto.getCommission());
        }
        if (commissionAmount.compareTo(BigDecimal.ZERO) != 1) {
            logger.info("公司抽成金额为0，公司抽成结束，orderUuid：{}", payVo.getOrderUuid());
            return BigDecimal.ZERO;
        }
        logger.info(payVo.getDescription() + "--公司抽成金额--" + commissionAmount.toString());
        //抽成金额大于订单余额，则全部抽完
        if (commissionAmount.compareTo(orderBalance) == 1) {
            commissionAmount = orderBalance;
        }

        //2. 处理流水和账户
        ResponseData<SysAccountCompanyDto> companyWrapper = sysAccountCompanyService.get(payVo.getDriverVo().getCompanyUuid());
        if (!companyWrapper.isSuccess() || companyWrapper.getData() == null) {
            logger.warn("获取不到公司账户信息，公司抽成结束，companyUuid:{}", payVo.getDriverVo().getCompanyUuid());
            return BigDecimal.ZERO;
        }
        //计算公司余额
        SysAccountCompanyDto accountCompanyDto = companyWrapper.getData();
        BigDecimal companyBalance = Optional.ofNullable(accountCompanyDto.getCashBalance()).orElse(BigDecimal.ZERO);

        //公司收入抽成流水
        FinanceOrderDto commissionFinance = new FinanceOrderDto();
        commissionFinance.setUuid(StringUtils.buildUUID());
        commissionFinance.setOrderUuid(payVo.getOrderUuid());
        commissionFinance.setAppid(payVo.getAppid());
        commissionFinance.setOrderUuid(payVo.getOrderUuid());
        commissionFinance.setCourse(FinanceConstant.COMPANY_EXTRACT);
        commissionFinance.setAmount(commissionAmount);
        commissionFinance.setFinanceNumber(StringUtils.buildUUID());
        // 平台->公司
        commissionFinance.setFinanceInflow(payVo.getDriverVo().getCompanyUuid());
        commissionFinance.setFinanceOutflow(sysAccountPlatformDto.getUuid());
        commissionFinance.setDescription("平台支付企业抽成" + commissionAmount + "元给公司");
        commissionFinance.setHappenTime(new Date());
        commissionFinance.setCreateOn(new Date());
        commissionFinance.setCreateBy(payVo.getUserUuid());
        commissionFinance.setStartBalance(companyBalance);
//        companyBalance = companyBalance.add(commissionAmount).setScale(1, BigDecimal.ROUND_HALF_UP);
        companyBalance = companyBalance.add(commissionAmount).setScale(2, BigDecimal.ROUND_HALF_UP);
        commissionFinance.setFinalBalance(companyBalance);
        financeOrderService.add(commissionFinance);

        //更新公司账户信息
        accountCompanyDto.setCashBalance(companyBalance);
        accountCompanyDto.setUpdateOn(new Date());
        CompletableFuture.runAsync(() -> sysAccountCompanyService.edit(accountCompanyDto)).whenComplete(
                (t, throwable) -> logger.info("公司抽成，更新公司账户成功，orderUuid:{}", payVo.getOrderUuid()));

        return commissionAmount;
    }

    private BigDecimal agentCommissionProcessor(Map<Integer, SysBusinessOrganizationDto> commissionMap, SysAccountPlatformDto
            sysAccountPlatformDto, PayVo payVo, BigDecimal orderFare, BigDecimal orderBalance) {
        // 订单金额小于等于0
        if (orderBalance.compareTo(BigDecimal.ZERO) != 1) {
            logger.info(payVo.getDescription() + "--代理抽成不执行--订单余额已被抽完");
            return BigDecimal.ZERO;
        }
        // 代理未配置抽成信息
        if (!commissionMap.containsKey(COMMISSION_LEVEL_AGENT)) {
            logger.info(payVo.getDescription() + "--代理抽成不执行--没有获取到代理抽成配置");
            return BigDecimal.ZERO;
        }
        SysBusinessOrganizationDto commissionDto = commissionMap.get(COMMISSION_LEVEL_AGENT);
        //1. 计算抽成金额
        BigDecimal commissionAmount;
        if (SysBusinessOrganizationDto.COMMISSION_TYPE_PERCENT == commissionDto.getCommissionType()) {
            // 百分比
//            commissionAmount = orderFare.multiply(BigDecimal.valueOf(commissionDto.getCommission()).multiply(new
//                    BigDecimal("0.01"))).setScale(1, BigDecimal.ROUND_HALF_UP);
            commissionAmount = orderFare.multiply(BigDecimal.valueOf(commissionDto.getCommission()).multiply(new
                    BigDecimal("0.01"))).setScale(2, BigDecimal.ROUND_HALF_UP);
        } else {
            // 固定比例
            commissionAmount = BigDecimal.valueOf(commissionDto.getCommission());
        }
        if (commissionAmount.compareTo(BigDecimal.ZERO) != 1) {
            logger.info("代理抽成金额为0，代理抽成结束，orderUuid：{}", payVo.getOrderUuid());
            return BigDecimal.ZERO;
        }
        logger.info(payVo.getDescription() + "--代理抽成金额--" + commissionAmount.toString());
        //抽成金额大于订单余额，则全部抽完
        if (commissionAmount.compareTo(orderBalance) == 1) {
            commissionAmount = orderBalance;
        }

        //2. 处理流水和账户
        ResponseData<SysAccountAgentDto> agentWrapper = sysAccountAgentService.get(payVo.getDriverVo().getAgentUuid());
        if (!agentWrapper.isSuccess() || agentWrapper.getData() == null) {
            logger.warn("获取不到代理账户信息，代理抽成结束，agentUuid:{}", payVo.getDriverVo().getAgentUuid());
            return BigDecimal.ZERO;
        }
        //计算代理余额
        SysAccountAgentDto accountAgentDto = agentWrapper.getData();
        BigDecimal agentBalance = Optional.ofNullable(accountAgentDto.getCashBalance()).orElse(BigDecimal.ZERO);
        //代理收入抽成流水
        FinanceOrderDto agentFinance = new FinanceOrderDto();
        agentFinance.setUuid(StringUtils.buildUUID());
        agentFinance.setBusinessUuid(payVo.getOrderUuid());
        agentFinance.setOrderUuid(payVo.getOrderUuid());
        agentFinance.setAppid(payVo.getAppid());
        agentFinance.setOrderUuid(payVo.getOrderUuid());
        agentFinance.setCourse(FinanceConstant.AGENT_EXTRACT);
        agentFinance.setAmount(commissionAmount);
        agentFinance.setFinanceNumber(StringUtils.buildUUID());
        // 平台 -> 代理
        agentFinance.setFinanceInflow(accountAgentDto.getUuid());
        agentFinance.setFinanceOutflow(sysAccountPlatformDto.getUuid());
        agentFinance.setDescription("平台支付代理抽成" + commissionAmount + "元给代理");
        agentFinance.setHappenTime(new Date());
        agentFinance.setCreateOn(new Date());
        agentFinance.setStartBalance(agentBalance);
//        agentBalance = agentBalance.add(commissionAmount).setScale(1, BigDecimal.ROUND_HALF_UP);
        agentBalance = agentBalance.add(commissionAmount).setScale(2, BigDecimal.ROUND_HALF_UP);
        agentFinance.setFinalBalance(agentBalance);
        agentFinance.setCreateBy(payVo.getUserUuid());
        financeOrderService.add(agentFinance);

        //更新代理账户信息
        accountAgentDto.setCashBalance(agentBalance);
        accountAgentDto.setUpdateOn(new Date());
        CompletableFuture.runAsync(() -> sysAccountAgentService.edit(accountAgentDto)).whenComplete(
                (t, throwable) -> logger.info("代理抽成，更新代理账户成功，orderUuid:{}", payVo.getOrderUuid()));
        return commissionAmount;
    }


    private BigDecimal platformCommissionProcessor(Map<Integer, SysBusinessOrganizationDto> commissionMap, SysAccountPlatformDto
            sysAccountPlatformDto, PayVo payVo, BigDecimal orderFare, BigDecimal orderBalance) {
        // 订单金额小于等于0
        if (orderBalance.compareTo(BigDecimal.ZERO) != 1) {
            logger.info(payVo.getDescription() + "--平台抽成不执行--订单余额已被抽完");
            return BigDecimal.ZERO;
        }
        // 平台未配置抽成信息
        if (!commissionMap.containsKey(COMMISSION_LEVEL_PLATFORM)) {
            logger.info(payVo.getDescription() + "--平台抽成不执行--没有获取到平台抽成配置");
            return BigDecimal.ZERO;
        }
        SysBusinessOrganizationDto commissionDto = commissionMap.get(COMMISSION_LEVEL_PLATFORM);
        //1. 计算抽成金额
        BigDecimal commissionAmount;
        if (SysBusinessOrganizationDto.COMMISSION_TYPE_PERCENT == commissionDto.getCommissionType()) {
            // 百分比
//            commissionAmount = orderFare.multiply(BigDecimal.valueOf(commissionDto.getCommission()).multiply(new BigDecimal("0.01"))).setScale(1, BigDecimal.ROUND_HALF_UP);
            commissionAmount = orderFare.multiply(BigDecimal.valueOf(commissionDto.getCommission()).multiply(new BigDecimal("0.01"))).setScale(2, BigDecimal.ROUND_HALF_UP);
        } else {
            // 固定金额
            commissionAmount = BigDecimal.valueOf(commissionDto.getCommission());
        }
        if (commissionAmount.compareTo(BigDecimal.ZERO) != 1) {
            logger.info("平台抽成金额为0，平台抽成结束，orderUuid：{}", payVo.getOrderUuid());
            return BigDecimal.ZERO;
        }
        logger.info(payVo.getDescription() + "--平台抽成金额--" + commissionAmount.toString());
        //抽成金额大于订单余额，则全部抽完
        if (commissionAmount.compareTo(orderBalance) == 1) {
            commissionAmount = orderBalance;
        }

        //2.计算平台附加费
        BigDecimal additionalFee = BigDecimal.valueOf(commissionDto.getAdditionalFee());
        // 附加费如果大于订单剩余余额，则全部扣完
        orderBalance = orderBalance.subtract(commissionAmount);
        if (orderBalance.compareTo(BigDecimal.ZERO) != 1) {
            additionalFee = BigDecimal.ZERO;
        }
        if (orderBalance.compareTo(additionalFee) < 0) {
            additionalFee = orderBalance;
        }

        //3. 处理流水和账户
        BigDecimal cashBalance = sysAccountPlatformDto.getCashBalance();
        //增加平台收入流水 流入方流出方都是平台 只增加流水 账户余额不操作
        FinanceOrderDto platFinance = new FinanceOrderDto();
        platFinance.setUuid(StringUtils.buildUUID());
        platFinance.setOrderUuid(payVo.getOrderUuid());
        platFinance.setAppid(payVo.getAppid());
        platFinance.setOrderUuid(payVo.getOrderUuid());
        platFinance.setCourse(FinanceConstant.PLATFORM_EXTRACT);
        platFinance.setAmount(commissionAmount);
        platFinance.setFinanceNumber(StringUtils.buildUUID());
        // 平台到平台
        platFinance.setFinanceInflow(sysAccountPlatformDto.getUuid());
        platFinance.setFinanceOutflow("");
        platFinance.setDescription("平台支付平台抽成" + commissionAmount + "元给平台");
        platFinance.setHappenTime(new Date());
        platFinance.setCreateOn(new Date());
        platFinance.setStartBalance(cashBalance);
//        cashBalance = cashBalance.add(commissionAmount).setScale(1, BigDecimal.ROUND_HALF_UP);
        cashBalance = cashBalance.add(commissionAmount).setScale(2, BigDecimal.ROUND_HALF_UP);
        platFinance.setFinalBalance(cashBalance);
        platFinance.setCreateBy(payVo.getUserUuid());
        financeOrderService.add(platFinance);

        //平台收入保险费流水
        if (additionalFee.compareTo(BigDecimal.ZERO) > 0) {
            logger.info("订单:{},扣取保险费用：{}", payVo.getOrderUuid(), additionalFee);
            FinanceOrderDto addFeeFinance = new FinanceOrderDto();
            addFeeFinance.setUuid(StringUtils.buildUUID());
            addFeeFinance.setOrderUuid(payVo.getOrderUuid());
            addFeeFinance.setAppid(payVo.getAppid());
            addFeeFinance.setOrderUuid(payVo.getOrderUuid());
            addFeeFinance.setCourse(FinanceConstant.DRIVER_ADDITIONAL_FEE);
            addFeeFinance.setAmount(additionalFee);
            addFeeFinance.setFinanceNumber(StringUtils.buildUUID());
            // 司机->平台
            addFeeFinance.setFinanceInflow(sysAccountPlatformDto.getUuid());
            addFeeFinance.setFinanceOutflow(payVo.getDriverVo().getUuid());
            addFeeFinance.setDescription("司机支付保险费" + additionalFee + "元给平台");
            addFeeFinance.setHappenTime(new Date());
            addFeeFinance.setCreateOn(new Date());
            addFeeFinance.setCreateBy(payVo.getUserUuid());
            addFeeFinance.setStartBalance(cashBalance);
//            cashBalance = cashBalance.add(additionalFee).setScale(1, BigDecimal.ROUND_HALF_UP);
            cashBalance = cashBalance.add(additionalFee).setScale(2, BigDecimal.ROUND_HALF_UP);
            addFeeFinance.setFinalBalance(cashBalance);
            financeOrderService.add(addFeeFinance);
        }
        return commissionAmount.add(additionalFee);
    }
}
